using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Abp.Application.Services.Dto;
using Abp.AspNetCore.Mvc.Authorization;
using Abp.AspNetCore.Mvc.Extensions;
using Abp.Domain.Repositories;
using FwProject.Controllers;
using FwProject.Core.Restaurant;
using FwProject.Restaurant;
using FwProject.Restaurant.Dto;
using FwProject.TypeNumber;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace FwProject.Web.Controllers
{

    public class RestaurantTypeNumberController : FwProjectControllerBase
    {
        private readonly IRestaurantAppService _restaurantAppService;
        private readonly IRestaurantTypeNumberAppService _restaurantTypeNumberAppService;
        private readonly ITypeNumberInfoAppService _typeNumberInfoAppService;
        private readonly IHostingEnvironment _env;

        public RestaurantTypeNumberController(IRestaurantAppService restaurantAppService, IHostingEnvironment env,
            IRestaurantTypeNumberAppService restaurantTypeNumberAppService, ITypeNumberInfoAppService typeNumberInfoAppService)
        {
            _typeNumberInfoAppService = typeNumberInfoAppService;
            _restaurantTypeNumberAppService = restaurantTypeNumberAppService;
            _restaurantAppService = restaurantAppService;
            _env = env;
        }

        /// <summary>
        /// 获取餐厅绑定的型号
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<JsonResult> GetTypeNumbers(Guid id)
        {
            var list = await _restaurantTypeNumberAppService.GetRestaurantTypeNumAsync(id);

            return Json(list);
        }
        public async Task<IActionResult> GetTypeNumbersView(Guid id)
        {
            var list = await _restaurantTypeNumberAppService.GetRestaurantTypeNumAsync(id);

            return PartialView(list);
        }
        /// <summary>
        /// 创建餐厅绑定的型号
        /// </summary>
        /// <param name="pageIdx"></param>
        /// <param name="begin"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public async Task<ActionResult> Create(Guid id)
        {
            var list = await _typeNumberInfoAppService.GetAllTypeNumber();
            ViewBag.TypeNumber = list.Select(o => new SelectListItem() { Text = o.Name, Value = o.Id.ToString() });
            var FactoryinfoId = (await _restaurantAppService.GetByIdAsync(id)).FactoryInfoId;
            return View(new RestaurantTypeNumberInput { Restaurantid=id,FactoryinfoId= FactoryinfoId });
        }
        public async Task<JsonResult> CreateTypeNumber(RestaurantTypeNumberInput input)
        {
            var model = await _restaurantTypeNumberAppService.Create(input);
            return Json(model);
        }
        /// <summary>
        /// 修改餐厅绑定的型号
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ActionResult> Edit(Guid id)
        {
            var entity = await _restaurantTypeNumberAppService.GetByIdAsync(id);
            ViewBag.Id = id;
            return View(entity);
        }

        [HttpPost]
        public async Task<JsonResult> EditTypeNumber(Guid id, RestaurantTypeNumberInput input)
        {
            await _restaurantTypeNumberAppService.Update(id, input);
            return Json(id);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<JsonResult> DeleteTypeNumber(Guid id)
        {
            await _restaurantTypeNumberAppService.Delete(id);
            return Json(id);
        }
    }
}
